﻿package com.gengine.core.call
{
    import flash.utils.*;

    public class Caller extends Object implements ICaller
    {
        private var _callerMap:Dictionary;
        private var _callerIng:Dictionary;
        private var _removeLater:Array;

        public function Caller()
        {
            this._callerMap = new Dictionary(true);
            this._callerIng = new Dictionary(true);
            this._removeLater = [];
            return;
        }// end function

        public function addCall(param1:Object, param2:Function) : void
        {
            var _loc_3:Dictionary = null;
            if (param2 != null)
            {
                _loc_3 = this.getlistByType(param1);
                _loc_3[param2] = param2;
            }
            return;
        }// end function

        public function hasCallFun(param1:Object, param2:Function) : Boolean
        {
            var _loc_3:* = this.getlistByType(param1);
            return _loc_3[param2];
        }// end function

        public function removeCall(param1:Object, param2:Function) : void
        {
            if (this._callerIng.hasOwnProperty(param1))
            {
                this._removeLater.push(param2);
                return;
            }
            var _loc_3:* = this._callerMap[param1] as Dictionary;
            if (_loc_3)
            {
                delete _loc_3[param2];
            }
            return;
        }// end function

        public function call(param1:Object, ... args) : Boolean
        {
            var _loc_4:Function = null;
            args = this._callerMap[param1] as Dictionary;
            if (args)
            {
                this._callerIng[param1] = param1;
                for each (_loc_4 in args)
                {
                    
                    _loc_4.apply(null, args);
                }
                delete this._callerIng[param1];
                while (this._removeLater.length > 0)
                {
                    
                    delete args[this._removeLater.shift()];
                }
            }
            return true;
        }// end function

        public function dispose(param1:Boolean = true) : void
        {
            var _loc_2:Dictionary = null;
            var _loc_3:String = null;
            for (_loc_3 in this._callerMap)
            {
                
                _loc_2 = this._callerMap[_loc_3] as Dictionary;
                _loc_2 = null;
                delete this._callerMap[_loc_3];
            }
            return;
        }// end function

        private function getlistByType(param1:Object) : Dictionary
        {
            if (param1 in this._callerMap)
            {
                return this._callerMap[param1];
            }
            var _loc_2:* = new Dictionary();
            this._callerMap[param1] = _loc_2;
            return _loc_2;
        }// end function

        public function removeCallByType(param1:Object) : void
        {
            var _loc_2:* = this._callerMap[param1] as Dictionary;
            if (_loc_2)
            {
                _loc_2 = null;
                delete this._callerMap[param1];
            }
            return;
        }// end function

        public function hasCall(param1:Object) : Boolean
        {
            return param1 in this._callerMap;
        }// end function

    }
}
